package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepPlanRemark;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanRemarkService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

@Tag(name = "EquipmentUpkeepPlanRemarkController", description = "保养计划-重要备注")
@RestController
@RequestMapping("/erp/equipment/upkeep/plan/remark")
public class EquipmentUpkeepPlanRemarkController {

    @Autowired
    private EquipmentUpkeepPlanRemarkService remarkService;

    @GetMapping("list")
    @Operation(summary = "重要备注列表", description = "重要备注列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepPlanRemark>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepPlanRemark> list = remarkService.list(EquipmentUpkeepPlanRemark.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "重要备注列表", description = "重要备注列表（分页）", method = "GET")
    public Result<Page<EquipmentUpkeepPlanRemark>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentUpkeepPlanRemark> pageResult = remarkService.listPage(EquipmentUpkeepPlanRemark.class, page, criteria);
        return Result.OK(pageResult);
    }



    @GetMapping("list/{planId}")
    @Operation(summary = "根据保养计划ID获取重要备注列表", description = "根据保养计划ID获取重要备注列表", method = "GET")
    public Result<EquipmentUpkeepPlanRemark> getById(@PathVariable(name = "planId", required = true) String planId) {
        EquipmentUpkeepPlanRemark remark = remarkService.getById(EquipmentUpkeepPlanRemark.class, planId);
        return Result.OK(remark);
    }

    @RequestMapping("add")
    @Operation(summary = "新增重要备注", description = "新增重要备注")
    public Result<EquipmentUpkeepPlanRemark> add(@RequestBody EquipmentUpkeepPlanRemark remark) {
        remarkService.save(remark);
        return Result.OK(remark);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑重要备注", description = "编辑重要备注", method = "PUT")
    public Result<EquipmentUpkeepPlanRemark> edit(@RequestBody EquipmentUpkeepPlanRemark remark) {
        remarkService.update(remark);
        return Result.OK(remark);
    }
    @DeleteMapping(value = "delete/{ids}")
    @Operation(summary = "批量删除", description = "批量删除", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "ids", required = true) String ids) {
        Criteria criteria = Criteria.create();
        criteria.addCriterion("id", Operator.IN, Arrays.asList(ids.split(",")));
        this.remarkService.removeBatch(EquipmentUpkeepPlanRemark.class,criteria);
        return Result.OK("批量删除成功!");
    }
}
